package com.rose.redis._distributed_lock.impl;

import com.rose.redis._distributed_lock.Lock;
import com.rose.redis._distributed_lock.LockConstant;
import org.redisson.api.RLock;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * Redis实现分布式锁，参考：https://github.com/redisson/redisson/wiki/8.-%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E5%92%8C%E5%90%8C%E6%AD%A5%E5%99%A8
 * </p>
 *
 * @author: zhu.chen
 * @date: 2020/12/30
 * @version: v1.0.0
 */
//@Component
public class DistributeLock extends LockConstant implements Lock {

    @Override
    public boolean tryLock(RLock lock) throws InterruptedException {
        return lock.tryLock(WAIT_TIME, LEASE_TIME, TimeUnit.SECONDS);
    }

    @Override
    public boolean tryLock(RLock lock, Long waitTime, Long leaseTime) throws InterruptedException {
        return lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS);
    }

    @Override
    public void unlock(RLock lock) {
        lock.unlock();
    }

}
